package pl.tssoft.math.core.parser;

/**
 *
 * @author tserafin
 */
public class InputIterator {

    private final char[] input;
    private int position = 0;

    public InputIterator(String input) {
        this(input.toCharArray());
    }

    public InputIterator(char[] input) {
        this.input = input;
    }

    public boolean isPresent() {
        return position < input.length;
    }

    public Character get() {
        return position < input.length ? input[position] : null;
    }

    public Character getNext() {
        return position + 1 < input.length ? input[position + 1] : null;
    }

    public void forward() {
        if (position == input.length) {
            return;
        }
        position++;
    }

    public Character forwardAndGet() {
        forward();
        return get();
    }

    public Character getAndForward() {
        Character c = get();
        forward();
        return c;
    }

    public int getPosition() {
        return position;
    }

    public Character getPrevious() {
        return position - 1 >= 0 ? input[position - 1] : null;
    }

    public void omitWhitespaces() {
        while (isPresent() && Character.isWhitespace(get())) {
            forward();
        }
    }

    public int getPreviousNonWhitespacePosition() {
        int p = getPosition();
        while (p > 0) {
            p--;
            if (!Character.isWhitespace(input[p])) {
                break;
            }
        }
        return p;
    }
}
